/*
** my_list.c for  in /home/georgi_v/rendu/PSU_2013_my_printf/libs
** 
** Made by georgi_v
** Login   <georgi_v@epitech.net>
** 
** Started on  Sat Nov 16 20:21:26 2013 georgi_v
** Last update Fri Dec  6 19:52:47 2013 georgi_v
*/

#include "my.h"

int	get_list_size_ex(t_list_ex *list)
{
  int	cnt;

  cnt = 0;
  while (list != NULL)
    {
      list = list->next;
      cnt++;
    }
  return (cnt);
}

int		add_elem_front_ex(t_list_ex **list, char *str)
{
  t_list_ex	*elem;

  if ((elem = malloc(sizeof(*elem))) == NULL)
    return (1);
  if ((elem->str = malloc(sizeof(my_strlen(str)))) == NULL)
    return (1);
  elem->str = str;
  elem->color = 0;
  elem->next = *list;
  *list = elem;
  return (0);
}

int		add_elem_end_ex(t_list_ex **list, char *str)
{
  t_list_ex	*current;
  t_list_ex	*elem;

  current = *list;
  if ((elem = malloc(sizeof(*elem))) == NULL)
    return (1);
  if ((elem->str = malloc(sizeof(my_strlen(str)))) == NULL)
    return (1);
  elem->str = str;
  elem->next = NULL;
  if (current == NULL)
    {
      *list = elem;
      return (0);
    }
  else
    {
      while (current->next != NULL)
	current = current->next;
      current->next = elem;
      return (0);
    }
}

void		delete_elem_ex(t_list_ex **list, int index)
{
  t_list_ex	*elem = *list;
  int		size = get_list_size_ex(*list);
  int		cnt;

  cnt = 0;
  if (size == 0)
    *list = NULL;
  else
    {
      if (index == 0)
	*list = (*list)->next;
      else
	{
      	  while (cnt < index - 1)
      	    {
      	      cnt++;
      	      elem = elem->next;
      	    }
	  if (index == size - 1)
	    elem->next = NULL;
	  else
	    if (elem->next->next != NULL)
	      elem->next = elem->next->next;
	}
    }
}

char	*get_elem_str_ex(t_list_ex *list, int index)
{
  int	cnt;

  cnt = 0;
  while (cnt < index)
    {
      cnt++;
      list = list->next;
    }
  return (list->str);
}

int	get_elem_color_ex(t_list_ex *list, int index)
{
  int	cnt;

  cnt = 0;
  while (cnt < index)
    {
      cnt++;
      list = list->next;
    }
  return (list->color);
}

void	set_elem_color_ex(t_list_ex *list, int index, int color)
{
  int	cnt;

  cnt = 0;
  while (cnt < index)
    {
      cnt++;
      list = list->next;
    }
  list->color = color;
}
